unit Main;

interface

uses
  Windows, Messages, SysUtils, Variants, Classes, Graphics, Controls, Forms,
  Dialogs, StdCtrls, Buttons, ExtCtrls, SkypeControlConst, SkypeControl,
  TntStdCtrls, ComCtrls, ToolWin, ImgList, XPMan, Smiles, Menus, About, Link,
  TntMenus, Clipbrd, TntComCtrls, ShellApi;

const N = 18;
      CONNECT:boolean = True; //For debug puposes. Must be true for program to work
      HTTP = 'http://code.google.com/p/moodfactory/';
      VER = '2.0';
      PAYPAL = 'https://www.paypal.com/cgi-bin/webscr?cmd=_donations&business=hanecreations%40gmail%2ecom&item_name=Donation%20to%20Mood%20Factory&no_shipping=0&no_note=1&tax=0&currency_code=EUR&bn=PP%2dDonationsBF&charset=UTF%2d8';

type
  TTags = array [1..N] of string;

  TMainF = class(TForm)
    SC: TSkypeControl;
    Memo: TTntMemo;
    ToolBar1: TToolBar;
    BBold: TToolButton;
    BUnderline: TToolButton;
    BItalic: TToolButton;
    ImageList1: TImageList;
    BBlink: TToolButton;
    BSmile: TToolButton;
    BColor: TToolButton;
    XPManifest1: TXPManifest;
    ColorDialog1: TColorDialog;
    BCenter: TToolButton;
    BSize: TToolButton;
    PopupMenu1: TPopupMenu;
    N31: TMenuItem;
    N41: TMenuItem;
    N51: TMenuItem;
    N81: TMenuItem;
    N91: TMenuItem;
    N101: TMenuItem;
    N111: TMenuItem;
    N121: TMenuItem;
    N131: TMenuItem;
    N141: TMenuItem;
    N151: TMenuItem;
    N161: TMenuItem;
    BLink: TToolButton;
    TntMainMenu1: TTntMainMenu;
    File1: TTntMenuItem;
    Exit1: TTntMenuItem;
    Help1: TTntMenuItem;
    About1: TTntMenuItem;
    SetB: TTntButton;
    GetB: TTntButton;
    Edit1: TTntMenuItem;
    Insert1: TTntMenuItem;
    Undo1: TTntMenuItem;
    N1: TTntMenuItem;
    Cut1: TTntMenuItem;
    Copy1: TTntMenuItem;
    Paste1: TTntMenuItem;
    Delete1: TTntMenuItem;
    SelectAll1: TTntMenuItem;
    Bold1: TTntMenuItem;
    Underline1: TTntMenuItem;
    Italic1: TTntMenuItem;
    Centered1: TTntMenuItem;
    FontSize1: TTntMenuItem;
    FontColor1: TTntMenuItem;
    Blink1: TTntMenuItem;
    Smile1: TTntMenuItem;
    Link1: TTntMenuItem;
    StatusBar: TTntStatusBar;
    Timer1: TTimer;
    N13: TTntMenuItem;
    N12: TTntMenuItem;
    N11: TTntMenuItem;
    N10: TTntMenuItem;
    N9: TTntMenuItem;
    N8: TTntMenuItem;
    N7: TTntMenuItem;
    N6: TTntMenuItem;
    N5: TTntMenuItem;
    N4: TTntMenuItem;
    N3: TTntMenuItem;
    N2: TTntMenuItem;
    HomePage1: TTntMenuItem;
    Donate1: TTntMenuItem;
    procedure Timer1Timer(Sender: TObject);
    procedure GetBClick(Sender: TObject);
    procedure SetBClick(Sender: TObject);
    procedure N31Click(Sender: TObject);
    procedure FormClose(Sender: TObject; var Action: TCloseAction);
    procedure FormCreate(Sender: TObject);
    procedure ProcessClick(Sender: TObject);
    procedure SetStatusText(Sender: TObject);
    procedure ClearStatusText(Sender: TObject);
  private
    //
  public
    SS: string[11];
    AA, AA2, AA3: WideString;
    Tags: TTags;
    Buffer: WideString;
    OnLine: boolean;
    GotPong: boolean;
    procedure Add(str: WideString);
    procedure Cut;
    procedure Paste(index: byte);
    function ColorToHtml(DColor:TColor):string;
    procedure Pong(Sender: TObject);
  end;

var
  MainF: TMainF;

implementation

{$R *.dfm}

procedure TMainF.FormCreate(Sender: TObject);
begin
  //Fill tags array
  Tags[1]  := '<b>';
  Tags[2]  := '</b>';
  Tags[3]  := '<u>';
  Tags[4]  := '</u>';
  Tags[5]  := '<i>';
  Tags[6]  := '</i>';
  Tags[7]  := '<center>';
  Tags[8]  := '</center>';
  Tags[9]  := '<blink>';
  Tags[10] := '</blink>';
  Tags[11] := '<font color="';
  Tags[12] := '</font>';
  Tags[13] := '<font size="';
  Tags[14] := '</font>';
  Tags[15] := '<ss type="';
  Tags[16] := '</ss>';
  Tags[17] := '<a href="';
  Tags[18] := '</a>';

  GotPong := False;

  //Connect to skype
  if CONNECT then
    begin
      SC.AttachToSkype(4);
      SC.AutoReAttach := True;
    end;

  SC.OnPong := Pong;
end;

procedure TMainF.Add(str: WideString);
begin
  Memo.Text := Memo.Text + str;
  Memo.SelStart := Memo.GetTextLen;
  Memo.SetFocus;
end;

procedure TMainF.Pong(Sender: TObject);
begin
  GotPong := True;
end;

procedure TMainF.Timer1Timer(Sender: TObject);
begin
  OnLine := GotPong;

  if OnLine then
    StatusBar.Panels[0].Text := 'Connected'
  else
    StatusBar.Panels[0].Text := 'Disconnected';

  SC.PingSkype;
  GotPong := False;
end;

procedure TMainF.ClearStatusText(Sender: TObject);
begin
  StatusBar.Panels[1].Text := '';
end;

function TMainF.ColorToHtml(DColor:TColor):string;
var tmpRGB: TColorRef;
begin
  tmpRGB := ColorToRGB(DColor);
  Result := Format('#%.2x%.2x%.2x',
                  [GetRValue(tmpRGB),
                   GetGValue(tmpRGB),
                   GetBValue(tmpRGB)]);
end;

procedure TMainF.Cut;
begin
  SetLength(Buffer, 0);
  if Memo.SelLength > 0 then
  begin
    Buffer := Memo.SelText;
    Memo.SelText := '';
  end;
end;

procedure TMainF.Paste(index: byte);
begin
  case index of
    1, 3, 5, 7, 9: begin
      Cut;
      Memo.SelText := Tags[index] + Buffer + Tags[index + 1];
    end;
    11: if ColorDialog1.Execute then
      begin
        Cut;
        Memo.SelText := Tags[index] + ColorToHtml(ColorDialog1.Color) + '">' + Buffer + Tags[index + 1];
      end;
    15: begin
      SetLength(SS, 0);

      Application.CreateForm(TSmileF, SmileF);
      try
        SmileF.ShowModal;
      finally
        SmileF.Free;
      end;

      if Length(SS) > 0 then
      begin
        Cut;
        Memo.SelText := Tags[index] + SS + '">' + Buffer + Tags[index + 1];
      end;
    end;
    17: begin
      SetLength(AA, 0);
      SetLength(AA2, 0);
      SetLength(AA3, 0);

      Application.CreateForm(TLinkF, LinkF);
      try
        LinkF.ShowModal;
      finally
        LinkF.Free;
      end;

      if (Length(AA) > 0) and (AA <> 'http://') then
      begin
        Cut;
        if Buffer <> AA3 then
          Buffer := AA3;
        Memo.SelText := Tags[index] + AA + '" alt="' + AA2 + '">' + Buffer + Tags[index + 1];
      end;
    end;
  end;
end;

procedure TMainF.ProcessClick(Sender: TObject);
begin
  //ToolBar
  if Sender.ClassType = TToolButton then
    Paste(TToolButton(Sender).Tag);

  //Main menu
  if Sender.ClassType = TTntMenuItem then
  case TMenuItem(Sender).Tag of
    1: Close;
    2: TAboutF.Execute(False);
    3: begin
      Undo1.Enabled := Memo.CanUndo;
      Cut1.Enabled := (Memo.SelLength > 0);
      Copy1.Enabled := (Memo.SelLength > 0);
      Paste1.Enabled := Clipboard.HasFormat(CF_TEXT);
      Delete1.Enabled := (Memo.SelLength > 0);
      SelectAll1.Enabled := (Memo.GetTextLen > 0);
    end;
    4: Memo.Undo;
    5: Memo.CutToClipboard;
    6: Memo.CopyToClipboard;
    7: Memo.PasteFromClipboard;
    8: Memo.SelText := '';
    9: Memo.SelectAll;
    10: BBold.Click;
    11: BUnderline.Click;
    12: BItalic.Click;
    13: BCenter.Click;
    14: BColor.Click;
    15: BBlink.Click;
    16: BSmile.Click;
    17: BLink.Click;
    18: N31.Click;
    19: N41.Click;
    20: N51.Click;
    21: N81.Click;
    22: N91.Click;
    23: N101.Click;
    24: N111.Click;
    25: N121.Click;
    26: N131.Click;
    27: N141.Click;
    28: N151.Click;
    29: N161.Click;
    30: ShellExecute(Handle, 'Open', HTTP, nil, nil, SW_SHOW);
    31: ShellExecute(Handle, 'Open', PAYPAL, nil, nil, SW_SHOW);
  end;
end;

procedure TMainF.SetBClick(Sender: TObject);
begin
  if OnLine then
    sc.SendData('SET PROFILE RICH_MOOD_TEXT ' + UTF8Encode(Memo.Text));

  ActiveControl := Memo;
end;

procedure TMainF.SetStatusText(Sender: TObject);
begin
  if Sender.ClassType = TToolButton then
    StatusBar.Panels[1].Text := TToolButton(Sender).Hint
  else if Sender.ClassType = TTntButton then
    StatusBar.Panels[1].Text := TTntButton(Sender).Hint;
end;

procedure TMainF.GetBClick(Sender: TObject);
var tmp: WideString;
begin
  if OnLine then
  begin
    tmp := UTF8Decode(sc.blGetProfileInfo(spfRichMood));
    Memo.Text := tmp;
  end;

  ActiveControl := Memo;
end;

procedure TMainF.N31Click(Sender: TObject);
begin
  Cut;
  Memo.SelText := Tags[13] + IntToStr(TMenuItem(Sender).Tag - 100) + '">' + Buffer + Tags[14];
end;

procedure TMainF.FormClose(Sender: TObject; var Action: TCloseAction);
begin
  //Disconnect from skype
  if CONNECT and SC.Attached then
    SC.DetachFromSkype;
end;


end.
